package net.wofly.right.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import net.wofly.common.condition.Condition;
import net.wofly.common.condition.ConditionRelate;
import net.wofly.common.condition.ConditionUtil;
import net.wofly.common.condition.FilterCondition;
import net.wofly.common.condition.Operation;
import net.wofly.common.json.XiNiaoJSON;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.util.PageHelper;
import net.wofly.common.util.ResultCode;
import net.wofly.common.util.ReturnBean;
import net.wofly.finance.expense.domain.ExpenseAccount;
import net.wofly.right.domain.UserReceivingAddr;
import net.wofly.right.repository.UserReceivingAddrRepository;
import net.wofly.right.service.IUserReceivingAddrService;
import net.wofly.sale.order.util.OrderType;

@Service("iUserReceivingAddrService")
@Transactional
public class UserReceivingAddrServiceImpl implements IUserReceivingAddrService {
	
	@Autowired
	public UserReceivingAddrRepository userReceivingAddrRepository;
	

	
	/**
	 * 流通订单 酒店订单 订货会订单 还货订单 签单订单  只能选择销售维护的客户
	 * 
	 * 会员订单只显示会员的客户
	 * 
	 * 宴会订单及团购订单 显示所有客户
	 * 
	 */
	@Override
	public List<UserReceivingAddr> getUserForOrder(String orderType, String saleUserID) {
		List<UserReceivingAddr> userReceivingAddrs = null ;
		
		if (orderType.equals(OrderType.流通订单.getValue().toString()) || 
				orderType.equals(OrderType.酒店订单.getValue().toString()) ||
				orderType.equals(OrderType.订货会流通订单.getValue().toString()) ||
				orderType.equals(OrderType.订货会酒店订单.getValue().toString()) ||
				orderType.equals(OrderType.还货订单.getValue().toString()) ||
				orderType.equals(OrderType.签单订单.getValue().toString()) ) {
			
			userReceivingAddrs = userReceivingAddrRepository.findByFollowUserID(saleUserID) ;
		}else if (orderType.equals(OrderType.会员订单.getValue().toString()) ||
				orderType.equals(OrderType.无款订单.getValue().toString())
				) {
			
			userReceivingAddrs = userReceivingAddrRepository.findByRoleID("201") ;
			
		}else if (orderType.equals(OrderType.宴会订单.getValue().toString()) ||
			orderType.equals(OrderType.团购订单.getValue().toString()) ) {
			
			userReceivingAddrs = userReceivingAddrRepository.findAll() ;
		}
		
		return userReceivingAddrs ;
	}
	
	@Override
	public List<UserReceivingAddr> findByFollowUserID(String followUserID) {
		return userReceivingAddrRepository.findByFollowUserID(followUserID);
	}
	
	@Override
	public List<UserReceivingAddr> findByRoleID(String roleID) {
		return userReceivingAddrRepository.findByRoleID(roleID);
	}
	
	@Override
	public List<UserReceivingAddr> findAll() {
		return userReceivingAddrRepository.findAll();
	}
	
	@Override
	public ReturnBean<PageWrapper<UserReceivingAddr>> findUserReceivingAddrByPage(String salesUserID, String customName, String linkMan, String mobile, Integer pageNum, Integer pageSize) {
		Condition condition = new Condition();
	    List<FilterCondition> filterConditions = new ArrayList<FilterCondition>();
	    
	    if (null != salesUserID) {
	    		filterConditions.add(new FilterCondition(salesUserID, "followUserID", ConditionRelate.and.name(), Operation.eq.name(), 1)) ;
	    }
	    
	    if (null != customName) {
	    		filterConditions.add(new FilterCondition(customName, "name", ConditionRelate.and.name(), Operation.like.name(), 2)) ;
	    }
	    
	    if (null != linkMan) {
	    		filterConditions.add(new FilterCondition(linkMan, "linkMan", ConditionRelate.and.name(), Operation.like.name(), 3)) ;
	    }
	    
	    if (null != mobile) {
	    		filterConditions.add(new FilterCondition(mobile, "mobile", ConditionRelate.and.name(), Operation.like.name(), 4)) ;
	    }
	    
	    condition.setFilterCondition(filterConditions);
	    
	    condition = ConditionUtil.buildCondition(XiNiaoJSON.toJSONString(condition), ExpenseAccount.class).getResult();
	    
	    Pageable pageable = new PageRequest(pageNum - 1, pageSize, new Sort(Sort.Direction.DESC, "name"));
	    Page<UserReceivingAddr> page = userReceivingAddrRepository.findAll(condition.getSpecification(), pageable);
		
	    return new ReturnBean<>(ResultCode.SUCCESS, "获取客户列表成功！", PageHelper.toPage(page));
	}
	    		
	
	
	
}
